package ij3d.geom;

/**
 * Class Point3D
 * Simple class for a point in 3D space
 *
 * @author     thomas
 * @created    9 janvier 2009
 */
public class Point3D {

	//
	// Fields
	//

	private double x = 0;
	private double y = 0;
	private double z = 0;

	//
	// Constructors
	//
	/**
	 *Constructor for the Point3D object
	 */
	public Point3D() { }


	/**
	 *Constructor for the Point3D object
	 *
	 * @param  a  Description of the Parameter
	 * @param  b  Description of the Parameter
	 * @param  c  Description of the Parameter
	 */
	public Point3D(double a, double b, double c) {
		x = a;
		y = b;
		z = c;
	}


	/**
	 *Constructor for the Point3D object
	 *
	 * @param  P  Description of the Parameter
	 */
	public Point3D(Point3D P) {
		x = P.getX();
		y = P.getY();
		z = P.getZ();
	}

	//
	// Methods
	//


	//
	// Accessor methods
	//

	/**
	 * Set the value of x
	 *
	 * @param  newVar  the new value of x
	 */
	public void setX(double newVar) {
		x = newVar;
	}


	/**
	 * Get the value of x
	 *
	 * @return    the value of x
	 */
	public double getX() {
		return x;
	}


	/**
	 * Set the value of y
	 *
	 * @param  newVar  the new value of y
	 */
	public void setY(double newVar) {
		y = newVar;
	}


	/**
	 * Get the value of y
	 *
	 * @return    the value of y
	 */
	public double getY() {
		return y;
	}


	/**
	 * Set the value of z
	 *
	 * @param  newVar  the new value of z
	 */
	public void setZ(double newVar) {
		z = newVar;
	}


	/**
	 * Get the value of z
	 *
	 * @return    the value of z
	 */
	public double getZ() {
		return z;
	}


	/**
	 *  Gets the vector3D attribute of the Point3D object
	 *
	 * @return    The vector3D value
	 */
	public Vector3D getVector3D() {
		return new Vector3D(x, y, z);
	}


	/**
	 *  Gets the position attribute of the Point3D object
	 *
	 * @return    The position value
	 */
	public Point3D getPosition() {
		return new Point3D(x, y, z);
	}

	//
	// Other methods
	//

	/**
	 *  Description of the Method
	 *
	 * @param  v  Description of the Parameter
	 */
	public void translate(Vector3D v) {
		x += v.getX();
		y += v.getY();
		z += v.getZ();
	}


	/**
	 *  Description of the Method
	 *
	 * @param  other  Description of the Parameter
	 * @return        Description of the Return Value
	 */
	public double distance(Point3D other) {
		return Math.sqrt((x - other.getX()) * (x - other.getX()) + (y - other.getY()) * (y - other.getY()) + (z - other.getZ()) * (z - other.getZ()));
	}


	/**
	 *  Description of the Method
	 *
	 * @param  other  Description of the Parameter
	 * @return        Description of the Return Value
	 */
	public double distanceSquare(Point3D other) {
		return ((x - other.getX()) * (x - other.getX()) + (y - other.getY()) * (y - other.getY()) + (z - other.getZ()) * (z - other.getZ()));
	}


	/**
	 *  Description of the Method
	 *
	 * @return    Description of the Return Value
	 */
    @Override
	public String toString() {
		return ("(" + x + " " + y + " " + z + ")");
	}

    /**
     *
     * @param wr
     */
    public void xmlWrite(java.io.Writer wr)
    {
        try
        {
             wr.write("<Point3D x=\"" + x + "\" y=\"" + y + "\" z=\"" + z + "\" />\n");
             wr.flush();

        }
        catch (java.io.IOException e) 
        {System.out.println("error in Point3D::XmlWrite " +e);}

    }
}


